﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Galaxy.ModuleInterface;
using System.IO;
using System.Reflection;

namespace Galaxy.ModuleInspector.ModuleLoader
{
    public partial class ModuleLoader_Form : Form
    {
        /* --------------------------------------------------------------------------------------------- */

        /* --------------------------------------------------------------------------------------------- */

        private Assembly f_CurrentAssembly;
        private Dictionary<string, Type> f_ModuleList = new Dictionary<string, Type>();

        /* --------------------------------------------------------------------------------------------- */

        protected Assembly CurrentAssembly
        {
            get { return f_CurrentAssembly; }
            set
            {
                if(f_CurrentAssembly == value) return;
                f_CurrentAssembly = value;
                ShowAssembly(f_CurrentAssembly);
            }
        }

        private string FilePath
        {
            get { return Path_MenuStrip.Text; }
            set { Path_MenuStrip.Text = value; }
        }

        private List<Type> KnownModuleTypes
        {
            get
            {
                return new List<Type>(f_ModuleList.Values);
            }
            set
            {
                f_ModuleList.Clear();
                if(value != null)
                    value.ForEach(delegate(Type ModuleType)
                    { f_ModuleList.Add(ModuleType.Name, ModuleType); });
            }
        }

        public Dictionary<string, Type> KnownModulesType
        {
            get { return f_ModuleList; }
        }

        /* --------------------------------------------------------------------------------------------- */

        public ModuleLoader_Form()
        {
            InitializeComponent();

        }


        /* --------------------------------------------------------------------------------------------- */

        private void LoadFile_OnClick(object sender, EventArgs e)
        {

        }

        public bool LoadFile(string FileName)
        {
            if(this.InvokeRequired)
                return (bool)this.Invoke(new Predicate<string>(LoadFile));
            else
                return Load(FileName);
        }

        private bool Load(string FileName)
        {
            if(!File.Exists(FileName)) return false;
            try
            {
                Assembly lv_LoadedAssembly = Assembly.LoadFile(FileName);
            }
            catch(Exception)
            {
                return false;
            }
            return true;
        }

        protected void ShowAssembly(Assembly assembly)
        {
            if(assembly == null) { Clear(); return; }
            FilePath = assembly.FullName;
            List<Type> lv_AllAsemblyTypes = new List<Type>(assembly.GetExportedTypes());
            List<Type> lv_KnownModules = lv_AllAsemblyTypes.FindAll(delegate(Type ObjectType)
            {
                if(ObjectType.IsClass && (ObjectType.IsSubclassOf(typeof(GalaxyModule))) ||
                    Array.Exists<Type>(ObjectType.GetInterfaces(), typeof(IGalaxyModule).Equals))
                    return true;
                return false;
            });

        }

        protected void Clear()
        {
            FilePath = "";
        }

        private void OpenFileDialog_Click(object sender, EventArgs e)
        {
            OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.ShowDialog();
        }

        /* --------------------------------------------------------------------------------------------- */
    }
}